import useUserInfo from '@/store/user.js'

import { createRouter, createWebHashHistory } from 'vue-router'
import layout from '../pages/layout.vue'
const router = createRouter({
  history: createWebHashHistory(),
  scrollBehavior: () => {
    return {
      top: 0
    }
  },
  routes: [
    {
      path: '',
      component: () => import('@/pages/index/index.vue'),
    },
    {
      path: '/login',
      component: () => import('@/pages/login.vue'),
    },
    {
      path: '/quit',
      component: () => import('@/pages/quit.vue'),
    },
    {
      path: '/custom-detail',
      component: layout,
      children: [{
        path: "",
        component: () => import('@/pages/custom/custom-detail.vue')
      }
      ]
    },
    {
      path: '/custom',
      component: layout,
      children: [{
        path: "",
        component: () => import('@/pages/custom/index.vue')
      }
      ]
    },
    {
      path: '/course',
      component: layout,
      children: [{
        path: "",
        component: () => import('@/pages/course/index.vue')
      }]
    },

    {
      path: '/student',
      component: layout,
      children: [
        {
          path: '',
          component: () => import('@/pages/student/index.vue')
        }]
    },
    {
      path: '/settings',
      component: layout,
      children: [
        {
          path: '',
          component: () => import('@/pages/settings.vue')
        }]
    },
    {
      path: '/class',
      component: layout,
      children: [
        {
          path: '',
          component: () => import('@/pages/class/index.vue')
        }, {
          path: 'class-detail',
          component: () => import('@/pages/class/class-detail.vue')
        }]
    },
    {
      path: '/my',
      component: layout,
      children: [
        {
          path: '',
          component: () => import('@/pages/my/index.vue')
        }]
    },
    {
      path: '/paper',
      component: layout,
      children: [
        {
          path: '',
          component: () => import('@/pages/paper/index.vue')
        },
        {
          path: "/paper-detail",
          component: () => import('@/pages/paper/paper-detail.vue')
        }]
    },
    {
      path: '/exam',
      component: layout,
      children: [
        {
          path: '',
          component: () => import('@/pages/exam/index.vue')
        }, {
          path: '/exam-detail',
          component: () => import('@/pages/exam/detail.vue')
        }]
    },

    {
      path: '/course-detail',
      component: layout,
      children: [{
        path: "",
        component: () => import('@/pages/course/course-detail.vue')
      }
      ]
    },
    {
      path: '/lesson-detail',
      component: layout,
      children: [
        {
          path: "",
          component: () => import('@/pages/lesson/lesson-detail.vue')
        }
      ]
    }
  ]
})
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  // 在这里执行您的逻辑，如身份验证、权限验证等
  if (whiteList.includes(to.path)) {
    // 如果要访问的路径是登录页面，则直接允许访问
    next();
  } else {
    const token = useUserInfo().data.token;
    // 如果要访问的路径不是登录页面，则需要进行身份验证或其他验证
    // 这里可以进行身份验证或其他验证，例如检查用户是否已经登录
    // 如果用户已经登录，则允许访问；否则，重定向到登录页面
    if (token) {
      next();
    } else {
      next('/login');
    }
  }
});

export default router
